/**
 * Created by wuhuan on 15/12/4.
 */
// 所有模块都通过 define 来定义
define(function (require, exports, module) {

    var Model = require('/plugins/extend/Model');

  //快捷支付，缺少获取验证码action
	$("#getPhoneCodeEdit").on("click",function(){
		var _that = this;
		var jdata = {};
		var params = ['dictBankId','bankNo','mobilephone','province','city'];
		for(var i=0,len=params.length;i<len;i++){
			var jval = $("#bindingBankForm").find("[name="+params[i]+"]").val();
			jdata[params[i]] = jval;
		}
		if($("#bankNo").val().length<15){
			layer.alert("请正确填写银行卡号");
			return false;
		}else if(!/^1[3578]\d{9}$/.test($("#mobilephone").val())){
			layer.alert("请正确填写手机号码");
			return false;
		}
		
		$.ajax({
			url:"/app/pay/sendBindBankCode.html",
			type:"get",
			dataType:"json",
			data:jdata,
			success:function(data){
				if(data.resCode==1){
					get_code_time(_that);
					$("#myticket").val(data.ticket);
					layer.alert("发送成功");
				}else{
					layer.alert(data.resMsg);
				}
			}
		});
	});
	
	$("#getPhoneCodeEdit2").on("click",function(){
		var _that = this;
		if(!/^1[3578]\d{9}$/.test($("#mobilephone").val())){
			layer.alert("请正确填写手机号码");
			return false;
		}
		$.ajax({
			url:"/app/pay/bindPhone.html",
			type:"get",
			dataType:"json",
			data:{
				bankNo:$("#hidbankno").val(),
				mobilePhone:$("#mobilephone").val()
			},
			success:function(data){
				if(data.resCode==1){
					get_code_time(_that);
					layer.alert("发送成功");
				}else{
					layer.alert(data.resMsg);
				}
			}
		});
	});
    
    $.get("../../../app/user/accountBank.html",function(data){
    	var arr = data.data;
    	var options = [];
    	for(var i=0,len=arr.length;i<len;i++){
    		options.push("<option value='"+arr[i].id+"'>"+arr[i].name+"</option>");
    	}
    	
    	$("#dictBankId").html(options.join(""));
    });
    
    


    $("#bindingBankForm").validate({
        rules: {
            'bankname': {
                required: true
            },
            'dictBankId': {
                required: true
            },
            'bankNo': {
                required: true,
                isBankCardNo:true
            },
            'comfirmAccount': {
                required: true,
                equalTo: "#bankNo"
            },
            'mobilephone':{
            	required: true,
            	isMobile:true
            },
            'code':{
            	required: true
            }
        },
        messages: {
        	'bankname': {
        		required:"请输入银行名称"
            },
            'dictBankId': {
            	required:"请选择开户银行"
            },
            'bankNo': {
            	required:"请输入银行卡号"
            },
            'comfirmAccount': {
            	required:"请确认银行卡号",
                equalTo: "两次输入不一致"
            },
            'mobilephone':{
            	required: "请输入手机号码",
            	isMobile:"手机号码格式错误"
            },
            'code':{
            	required: "请输入手机验证码"
            }
        },
        submitHandler: function (form, event, validator) {
            $(form).ajaxSubmit(function (data) {

                if(!Model.showMsg(data)){
                    return;
                }else{
                    window.location.href = "/mobile/usercenter/security/setting.html";
                }

            })
        }
    });
    var wait = 60;
	get_code_time = function (elem) {
		if (wait == 0) {
			$(elem).text("重新发送");
			$(elem).next().hide();
			$(elem).show();
			wait = 60;
		} else {
			$(elem).next().text("("+wait+")秒后重新获取");
			$(elem).hide();
			$(elem).next().show();
			wait--;
			setTimeout(function() {
				get_code_time(elem);
			}, 1000);
		}
	};
    //加载城市选择插件
    require.async('jquery-citySelect/jquery.cityselect',function(){
        $("#city").citySelect({
            url:"../../default/js/jquery-citySelect/city_sina.json",
            prov:"浙江", //省份
            city:"杭州", //城市
            dist:"西湖区", //区县
            required:true,
            nodata:"none" //当子集无数据时，隐藏select
        });
    })
});